# -*- coding: utf-8 -*-

'''
Initial the global logger:
There are two loggers here, one is print to screen
    and one output to a file.
Change setLevel to filter which level of information
    you want
'''

import logging
import logging.handlers
import tcp_config as config


def init():
    # config logger with name 'orion.kernel'
    logger = logging.getLogger(config.LOG_NAME)
    logger.setLevel(logging.DEBUG)
    # create file handler with debug message
    file_handler = logging.handlers.TimedRotatingFileHandler(config.LOG_FILE_LOCATION, 
                                                             config.LOG_FILE_GENERATE_MEASURE, 
                                                             config.LOG_FILE_GENERATE_INTERVAL, 
                                                             config.LOG_FILE_BACKUP_COUNT)
    file_handler.setLevel(logging.DEBUG)
    # create a console handler with debug message
    console_handler = logging.StreamHandler()
    console_handler.setLevel(logging.DEBUG)
    # setup a formatter and add to handlers
    formatter = logging.Formatter(config.LOG_OUTPUT_FORMATTER)
    file_handler.setFormatter(formatter)
    console_handler.setFormatter(formatter)
    # add handlers to logger
    logger.addHandler(file_handler)
    logger.addHandler(console_handler)
    
    return logger
